package com.guotai.meet.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.guotai.meet.entity.Agenda;
import com.guotai.meet.entity.UserSystem;
import com.guotai.meet.service.AgendaService;
import com.guotai.meet.utils.DateUtil;

@Controller
@RequestMapping("/agenda")
public class AgendaController {

	@Autowired
	private AgendaService agendaService;
	
	/**
	 * 跳转到新增议程信息页面
	 * @param request
	 * @return
	 * @author Information
	 */
	@RequestMapping("/agendaPage")
	public String agendaPage(HttpServletRequest request,String agendaId) {
		if((!StringUtils.isEmpty(agendaId))&&(!"".equals(agendaId))) {
			List<Agenda> agendaList = agendaService.queryAgendaList(null);
			request.setAttribute("agenda", agendaList.get(0));
		}else {
			request.setAttribute("agenda", new Agenda());
		}
		return "/agendaPage";
	}
	
	/**
	 * 非管理员首页
	 * @param request
	 * @return
	 * @author Information
	 */
	@RequestMapping("/homePage")
	public String homePage(HttpServletRequest request) {
		UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
		List<Agenda> agendaList = agendaService.queryAgendaList(null);
		
		if(null!=agendaList && agendaList.size()>0){
			request.setAttribute("agenda", agendaList.get(0));
		}else {
			request.setAttribute("agenda", new Agenda());
		}
		request.setAttribute("agendaList", agendaList);
		request.setAttribute("users", user);
		return "/home";
	}
	
	/**
	 * 跳转到议程列表
	 * @param request
	 * @return
	 * @author Information
	 */
	@RequestMapping("/agendaList")
	public String agendaList(HttpServletRequest request) {
		List<Agenda> agendaList = agendaService.queryAgendaList(null);
		request.setAttribute("agendaList", agendaList);
		return "/agendaList";
	}
    
	/**
	 * 上传议程图片及信息
	 * @param files
	 * @param request
	 * @return
	 * @author Information
	 */
	@RequestMapping("/uploadeAgenda")
	public String uploadeAgenda(@RequestParam("img") CommonsMultipartFile[] files,HttpServletRequest request,Agenda agenda) {
		UserSystem user=(UserSystem)request.getSession().getAttribute("loginUser");
		request.setAttribute("users", user);
		//String path = request.getContextPath();
	    String str =null;
	    String filename = "";
//	    String relust = "保存数据成功！";
	    try {
		    for(int i = 0;i<files.length;i++){
		        if(!files[i].isEmpty()){
//		            str = request.getSession().getServletContext().getRealPath("/")+"fileUpload\\temp"+ new Date().getTime() + files[i].getOriginalFilename();
		            str = request.getSession().getServletContext().getRealPath("/")+ "resources\\uploade";
		           // int pre = (int) System.currentTimeMillis();
		            filename=files[i].getOriginalFilename();
	            	 //这个工具类我把文件写入本地文件。
	                FileUtils.writeByteArrayToFile(new File(str,filename), files[i].getBytes(),true);
		        }
		    }
		    String dateTime = DateUtil.formatTimesTampDate(new Date());
		    
		    if(files.length > 0) {
		    	agenda.setContentImg("resources\\uploade\\"+filename);
		    }
		    if(StringUtils.isEmpty(agenda.getAgendaId())||("").equals(agenda.getAgendaId())) {
		    	String uid = UUID.randomUUID().toString();
		    	agenda.setAgendaId(uid);
		    	agenda.setCreateTime(dateTime);
		    	agendaService.insertSelective(agenda);
		    }else {
		    	agenda.setUpdateTime(dateTime);
		    	agendaService.updateByPrimaryKeySelective(agenda);
		    }
            return "/home";
	    } catch (IOException e) {
            e.printStackTrace();
//            relust = "网络错误，请稍后重试！";
            request.setAttribute("agenda", agenda);
            return "/agendaPage";
        }
	   
	}
}
